package ConnectMysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Statement_ResultSet_Test {
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("驱动程序加载成功");
		} catch (ClassNotFoundException e) {
			System.out.println("驱动加载失败");
		}
		String url = "jdbc:mysql://localhost/huangbiao";
		String user = "root";
		String password = "root";
		try {
			Connection con = DriverManager.getConnection(url, user, password);
			System.out.println("数据库连接成功");
			Statement ment = con.createStatement();
			System.out.println("Statement对象创建成功");
			ResultSet set = ment.executeQuery("select *from exam2 where math=45");
			System.out.println("ResultSet对象创建成功");
			while (set.next()) {
				String id = set.getString("id");
				System.out.print("编号为" + id + ":");
				System.out.print("成绩为" + set.getString("math") + ":");
				System.out.print("性别为" + set.getString("sex") + ":");
				System.out.println("喜欢的明星为" + set.getString("sname") + ":");
			}
		} catch (SQLException e) {
			System.out.println("数据库查询失败");
		}

	}
}
